package fei.yuzhi.com.loveflowers.utils;

import android.util.Log;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import fei.yuzhi.com.loveflowers.bean.ClassifyAllBean;
import fei.yuzhi.com.loveflowers.bean.ClassifyItemBean;
import fei.yuzhi.com.loveflowers.bean.ProductBean;
import fei.yuzhi.com.loveflowers.config.URLConfig;
import fei.yuzhi.com.loveflowers.http.RetrofitService;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2016/10/7.
 */
public class HttpUtils {
    public  static void getMapFromNet(final onGetMapFromNet getMapFromNet){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(URLConfig.URL.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Call<ResponseBody> call=retrofit.create(RetrofitService.class).getClassifyAll();
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String str=response.body().string();
                    List<ClassifyAllBean.DatasBean> list=JieXi.getClassifyAllData(str);
                    getMapFromNet.getNetList(list);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
    }

    public static void getClassifyItemData(int id,final onGetClassifyItem getClassifyItem){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(URLConfig.URL.BASE_URL_CLASSIFY)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Map<String,String> map=new HashMap<>();
        map.put("act","goods_class");
        map.put("op","goods_list");
        map.put("gc_id",id+"");
        map.put("version","4.2.0");
        map.put("client","android");
        Call<ClassifyItemBean> call=retrofit.create(RetrofitService.class).getClassifyItem(map);
       call.enqueue(new Callback<ClassifyItemBean>() {
           @Override
           public void onResponse(Call<ClassifyItemBean> call, Response<ClassifyItemBean> response) {
               ClassifyItemBean bean=response.body();
               getClassifyItem.getNetClassifyList(bean.getDatas().getClassList().getChild());
           }

           @Override
           public void onFailure(Call<ClassifyItemBean> call, Throwable t) {

           }
       });
    }
    public static void getProductDara(int page,String keyword,final onGetProduct getproduct){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(URLConfig.URL.BASE_URL_CLASSIFY)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Map<String,String> map=new HashMap<>();
        map.put("act","goods");
        map.put("op","goods_list");
        map.put("curpage",page+"");
        map.put("page","10");
        map.put("gc_id",keyword);
        map.put("version","4.2.0");
        map.put("client","android");
        Call<ProductBean> call=retrofit.create(RetrofitService.class).getProductBean(map);
        call.enqueue(new Callback<ProductBean>() {
            @Override
            public void onResponse(Call<ProductBean> call, Response<ProductBean> response) {
                ProductBean bean=response.body();
                getproduct.getNetProductList(bean.getDatas().getGoodsList());
            }

            @Override
            public void onFailure(Call<ProductBean> call, Throwable t) {

            }
        });
    }

    public static void getProductPriceDara(int page,String keyword,final onGetProductPrice getproductprice){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(URLConfig.URL.BASE_URL_CLASSIFY)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Map<String,String> map=new HashMap<>();
        map.put("act","goods");
        map.put("op","goods_list");
        map.put("curpage",page+"");
        map.put("page","10");
        map.put("gc_id",keyword);
        map.put("key","3");
        map.put("order","1");
        map.put("version","4.2.0");
        map.put("client","android");
        Call<ProductBean> call=retrofit.create(RetrofitService.class).getProductBean(map);
        call.enqueue(new Callback<ProductBean>() {
            @Override
            public void onResponse(Call<ProductBean> call, Response<ProductBean> response) {
                ProductBean bean=response.body();
                getproductprice.getNetProductList(bean.getDatas().getGoodsList());
            }

            @Override
            public void onFailure(Call<ProductBean> call, Throwable t) {

            }
        });
    }

    public static void getProductSaleDara(int page,String keyword,final onGetProductSale getproductsale){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(URLConfig.URL.BASE_URL_CLASSIFY)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Map<String,String> map=new HashMap<>();
        map.put("act","goods");
        map.put("op","goods_list");
        map.put("curpage",page+"");
        map.put("page","10");
        map.put("gc_id",keyword);
        map.put("key","1");
        map.put("order","2");
        map.put("version","4.2.0");
        map.put("client","android");
        Call<ProductBean> call=retrofit.create(RetrofitService.class).getProductBean(map);
        call.enqueue(new Callback<ProductBean>() {
            @Override
            public void onResponse(Call<ProductBean> call, Response<ProductBean> response) {
                ProductBean bean=response.body();
                getproductsale.getNetProductList(bean.getDatas().getGoodsList());
            }

            @Override
            public void onFailure(Call<ProductBean> call, Throwable t) {

            }
        });
    }

    public interface onGetMapFromNet{
        void getNetList(List<ClassifyAllBean.DatasBean> list);
    }
    public interface onGetClassifyItem{
        void getNetClassifyList(List<ClassifyItemBean.DatasBean.ClassListBean.ChildBean> list);
    }
    public interface onGetProduct{
        void getNetProductList(List<ProductBean.DatasBean.GoodsListBean> list);
    }
    public interface onGetProductPrice{
        void getNetProductList(List<ProductBean.DatasBean.GoodsListBean> list);
    }
    public interface onGetProductSale{
        void getNetProductList(List<ProductBean.DatasBean.GoodsListBean> list);
    }

}
